<?php

namespace app\api\controller;

use app\middleware\Auth;
use app\model\OrdersModel;
use app\model\UserModel;
use think\App;

class User extends Base {

    protected $middleware = [Auth::class];
    private $userModel;

    private $orderModel;

    private $uid;


    public function __construct(App $app){

        parent::__construct($app);

        $this->req = request()->param();

        $this->userModel = new UserModel();

        $this->orderModel = new OrdersModel();

        $this->uid = $this->isLogin();
    }


    /*
     * 获取用户信息
     * */
    public function getUserInfo(){

        return $this->succ($this->userModel->getUserInfoByUid($this->uid));
    }


    /*
     * 修改用户信息
     * */
    public function editUserInfo(){

        if(!$this->req["username"]){
            return $this->fail("昵称不能为空");
        }
        if(!$this->req["avatar"]){
            return $this->fail("头像不能为空");
        }

        if(!in_array($this->req["gender"],[0,1,2])){
            return $this->fail("请选择性别");
        }
        if(!$this->req["birthday"] || !verification_date($this->req["birthday"])){
            return $this->fail("请选择日期");
        }

        $data = [
            "username"=>$this->req["username"],
            "avatar"=>$this->req["avatar"],
            "gender"=>$this->req["gender"],
            "birthday"=>$this->req["birthday"],
            "mobile"=>$this->req["mobile"]
        ];
        $where = array_merge($data,["uid"=>$this->uid]);
        if($this->userModel->getUserInfoByWhere($where)){
            return $this->fail("没有任何修改");
        }


        $res = $this->userModel->updateUserInfo($this->uid,$data);

        if($res){

            $this->userModel->delUserCacheByUid($this->uid);

            return $this->succ("修改成功");
        }else{
            return $this->fail("修改失败");
        }
    }


    /*
      * 获取用户各类订单数量
      * */
    public function getUserAllStatusOrderNum(){
        $data =[];

        //待支付订单数量
        $where=[];
        $where[] = ['o.uid','=',$this->uid];
        $where[] = ['o.is_delete','=',0];
        $where[] = ['o.order_status','=',0];
        $where[] = ['o.aftersale_status','=',0];
        $data["nopayNum"] = $this->orderModel->getTotal($where);


        //待发货订单数量
        $where=[];
        $where[] = ['o.uid','=',$this->uid];
        $where[] = ['o.is_delete','=',0];
        $where[] = ['o.order_status','=',1];
        $where[] = ['o.aftersale_status','=',0];
        $data["noSenderNum"] = $this->orderModel->getTotal($where);


        //待收货订单数量
        $where=[];
        $where[] = ['o.uid','=',$this->uid];
        $where[] = ['o.is_delete','=',0];
        $where[] = ['o.order_status','IN',[2,3]];
        $where[] = ['o.aftersale_status','=',0];
        $data["noReceiverNum"] = $this->orderModel->getTotal($where);

        //待评价订单数量
        $where=[];
        $where[] = ['o.uid','=',$this->uid];
        $where[] = ['o.is_delete','=',0];
        $where[] = ['o.order_status','=',4];
        $where[] = ['o.aftersale_status','=',0];
        $data["noReviewsNum"] = $this->orderModel->getTotal($where);

        //售后订单数量
        $where=[];
        $where[] = ['o.uid','=',$this->uid];
        $where[] = ['o.is_delete','=',0];
        $where[] = ['o.aftersale_status','<>',0];
        $where[] = ['o.order_status','<>',-1];
        $data["aftersaleNum"] = $this->orderModel->getTotal($where);

        return $this->succ($data);
    }
}